<?php
declare (strict_types = 1);
namespace app\course\model\comment;

use app\common\model\BaseModel;
use app\user\model\user\Account;
use think\model\concern\SoftDelete;
use think\model\relation\BelongsTo;
use think\model\relation\HasMany;
use think\model\relation\HasOne;

/**
 * 定义课程点评回复模型
 */
class CommentReply extends BaseModel
{
    use SoftDelete;

    /**
     * 表名
     * @var string
     */
    protected $name = 'course_comment_reply';

    /**
     * 设置自动写入时间
     * @var boolean
     */
    protected $autoWriteTimestamp = true;

    /**
     * 定义字段类型
     * @var array
     */
    protected $type = [
        'id'          => 'integer',
        'delete_time' => 'integer',
        'create_time' => 'integer',
    ];

    public function user($externalPk = 'user_id'): BelongsTo
    {
        return $this->belongsTo(Account::class, $externalPk)->append(['avatar_url']);
    }

    public function replyUser(): BelongsTo
    {
        return $this->belongsTo(Account::class, 'reply_user_id')->bind(['reply_user'=>'nick_name']);
    }

    public function liked(): HasOne
    {
        return $this->hasOne(CommentReplyLike::class, 'course_comment_reply_id')->where('like_user_id', request()->mid);
    }

    public function replyLike(): HasMany
    {
        return $this->hasMany(CommentReplyLike::class);
    }
}
